package com.chaosyeb.server.controller;


import com.chaosyeb.server.pojo.Admin;
import com.chaosyeb.server.pojo.RespBean;
import com.chaosyeb.server.pojo.Role;
import com.chaosyeb.server.service.IAdminService;
import com.chaosyeb.server.service.IRoleService;
import com.chaosyeb.server.utils.AdminUtils;
import com.chaosyeb.server.vo.RegisterVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Chaos
 * @since 2021-12-04
 */
@RestController
@RequestMapping("/system/admin/")
public class AdminController {

    @Autowired
    private IAdminService adminService;
    @Autowired
    private IRoleService roleService;

    @ApiOperation(value = "获取所有操作员")
    @GetMapping("/")
    public RespBean getAllAdmins(String keywords){
        //keywords关键字，姓名
        return adminService.getAllAdmins(keywords);
    }

    @ApiOperation(value = "更新操作员信息")
    @PutMapping("/update")
    public RespBean updateAdmin(@RequestBody Admin admin){
        String username = AdminUtils.getCurrentAdmin().getUsername();
        System.out.println("===username===" + username);
        admin.setUpdateBy(username);
        if (adminService.updateById(admin)) {
            return RespBean.success("更新成功！");
        }
        return RespBean.error("更新失败！");
    }

    @ApiOperation(value = "删除操作员-逻辑删除")
    @DeleteMapping("/{id}")
    public RespBean deleteAdmin(@PathVariable("id") Integer id){
        if (adminService.removeById(id)) {
            return RespBean.success("删除成功！");
        }
        return RespBean.error("删除失败！");
    }

    @ApiOperation(value = "删除操作员-物理删除")
    @DeleteMapping("/delete/{id}")
    public RespBean deleteAdminY(@PathVariable Integer id){
        return adminService.deleteById(id);
    }

    @ApiOperation(value = "获取所有角色")
    @GetMapping("/roles")
    public List<Role> getAdminRoles(){
        return roleService.list();
    }

    @ApiOperation(value = "更新操作员角色权限")
    @PutMapping("/role")
    public RespBean updateAdminRole(Integer adminId, Integer[] rids){
        return adminService.updateAdminRole(adminId, rids);
    }

    @ApiOperation(value = "添加操作员-注册")
    @PostMapping("/register")
    public RespBean register(RegisterVO registerVO){
        return adminService.register(registerVO);
    }

}
